package com.servlet;

import com.alibaba.fastjson.JSON;
import com.dao.CardDao;
import com.dao.Impl.CardDaoImpl;
import com.entity.Card;
import com.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "/registerServlet/*")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String url = request.getRequestURI();
        String methon = url.substring(url.lastIndexOf("/")+1);
        String target = null;
        switch (methon){
            case "add":
                target=add(request,response);
                break;
            case "cardId":
                target=cardId(request,response);
                break;
            default:break;
        }
        if (target!=null){
            request.getRequestDispatcher(target).forward(request, response);
        }

    }


    /**
     * card表中levelID是会员等级
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */

    protected String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user=new User();
        Card card=new Card();
        return  null;
    }

    /**
    * @Description: 获取card的最后一个 //描述
    * @Param: [request, response]//参数
    * @return: java.lang.String
    * @Author: 岚氓//作者
    * @Date: 2020/9/7  8:35
    */
    protected String cardId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CardDao dao=new CardDaoImpl();
        int s = dao.cardIdEnd();
        ajaxOutput(response, JSON.toJSONString(s));
        return  null;
    }

    /**
     * 向前面传输json数据
     * @param response
     * @param jsonstr
     */
    private void ajaxOutput(HttpServletResponse response, String jsonstr) {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.println(jsonstr);
        out.close();
    }
}
